var basePath = getBasePath(); 
var modelName = "文件模板";
var modelLowClassName = "attachFiles";
var modelUpClassName = "AttachFiles";

$(function(){
	  //显示权限按钮
	  var parentUUID = $("input[name='parentUUID']").val();
   	  $.permission.showButton(parentUUID,"actionBtnArea");
   	  
   	 setTimeout(function(){
		//点击是否下载控制
    	  $(".downloadControlForm").each(function(i){
    		  $(this).find("input[type='radio']").bind("click",function(){
    			  var isDownload = $(this).val();
    			  var id = $(this).attr("id");
    			  console.log("isDownload:"+isDownload+",id:"+id);
    			  $.post(basePath+"/attachFiles/updateDownLoadFlag.do",{id:id,isDownload:isDownload},
  	      		    	function(jsonData){
  	      					if(jsonData.code=="00"){
  	      						 console.log(jsonData.msg);
  	      					}
  	      		    	},
  	      		    "json");
    		  })
    	  })
	  }, 500);
   	  
   	  //表格初始化
	  $.dataGridEasyUI.initTable({
		  pageId:"#"+modelLowClassName+"ListPage",
		  gridId:"#"+modelLowClassName+"List",
		  url:getBasePath()+modelLowClassName+'/get'+modelUpClassName+'List.do',
		  toolbarId:"#"+modelLowClassName+"ListPage .tb_toolbar",
		  columns:[[
  		            {field:'id',checkbox:true},
  		            {field:'isDownload',title:'下载控制',width:'10%',formatter: function(value,row,index){
                    	var formHtml = "<div class='downloadControlForm'>"+
                    	   "<form name='"+row.uuid+"' name='"+row.uuid+"'><input type=\"radio\" id='"+row.id+"' name=\"isDownload\" value=\"0\" #allowHtml#>允许"+
                    	   "<input type=\"radio\" name=\"isDownload\" value=\"1\" id='"+row.id+"' #notAllowHtml#>不允许</form>"+
                    	"</div>";
                    	var checkHtml = "checked=\"checked\"";
                    	console.log(value==1);
                    	if(value==0){
                    		formHtml = formHtml.replace("#allowHtml#",checkHtml);
                    		formHtml = formHtml.replace("#notAllowHtml#","");
                    	}else{
                    		formHtml = formHtml.replace("#allowHtml#","");
                    		formHtml = formHtml.replace("#notAllowHtml#",checkHtml);
                    	}
	  		  	    	return formHtml;
					}},
  		            {field:'fileName',title:'文件名字',width:'20%',formatter: function(value,row,index){
  		            	var downloadLink = getImgPath()+"/"+row.filePath;
	  		  	    	return "<a href='"+downloadLink+"' title='点我下载文件'><img src='"+getBasePath()+"/manage/images/1-11112R01533-51.png' align='center'/>"+value+"</a>";
					}},
  		            {field:'fileColumn',title:'所属目录',width:'10%'},
  		            {field:'typeName',title:'类型名称',width:'10%'},
                    {field:'fileSize',title:'文件大小',width:'10%',formatter: function(value,row,index){
                    	var fileSize = new Number(value);
	  		  	    	return (fileSize/1024/1024).toFixed(2)+"MB";
					}},
                    {field:'note',title:'文件说明',width:'10%'},
                    {field:'editTime',title:'创建时间',width:'15%'}
  		        ]]
	  });
  
  //新增按钮绑定事件
  $("#addDataLink").bind("click",function(){
	  window.location.href=getBasePath()+"manage/office/editAttachFiles.jsp";
	  /*$.dataGridEasyUI.initEditWindow({
		  url:getBasePath()+"manage/"+modelLowClassName+"/edit"+modelUpClassName+".jsp",
		  title:"添加"+modelName+"信息",
		  actionType:"add"
	  });*/
  });
  
  //更新按钮绑定事件
  $("#updateDataLink").bind("click",function(){
	  $.dataGridEasyUI.initEditWindow({
		  url:basePath+"manage/office/uploadDocModuleFile.jsp?id="+id+"&showType=window",
		  title:"编辑"+modelName+"",
		  actionType:"update"
	  });
  });
  
  //批量删除
  $("#batchDeleteLink").bind("click",function(){
	  $.dataGridEasyUI.batchDeleteData({
		  url:getBasePath()+"/"+modelLowClassName+"/batchDel"+modelUpClassName+"ForHS.do",
	  });
  });
  
  //单个删除
  $("#singleDeleteLink").bind("click",function(){
	  $.dataGridEasyUI.batchDeleteData({
		  url:getBasePath()+"/"+modelLowClassName+"/del"+modelUpClassName+".do",
	  });
  });
 
   //图片弹窗
	$(".fancybox-effects-a").fancybox({
		helpers: {
			title : {
				type : 'outside'
			},
			overlay : {
				speedOut : 0
			}
		}
	});
})

//查看详细信息
function showDetail(id){
  $.dataGridEasyUI.initEditWindow({
	  url:getBasePath() + ""+modelLowClassName+"/get"+modelUpClassName+"Detail.do?id="+id,
	  title:"查看"+modelName+"明细",
	  actionType:"detail",
	  width: 650,
	  height: 480
  })
}

//审核信息
function checkSingleData(){
	$.dataGridEasyUI.initEditWindow({
		  url:getBasePath() + "manage/notice/checkNotice.jsp?id=",
		  title:"审核"+modelName,
		  actionType:"check",
	      width: 650,
          height: 300
	});
}

//检索数据
function seachData(){
   var searChObj = new Object(); 
   var formId = "searchForm";
   $("#"+formId).find("input").each(function(i){
	   searChObj[$(this).attr("name")] = $(this).val();
   });
   
   $("#"+formId).find("select").each(function(i){
	   searChObj[$(this).attr("name")] = $(this).val();
   });
   tableObject.datagrid({
	     queryParams:searChObj
   })
}

//永久删除数据
function batchDelInDB(){
  $.dataGridEasyUI.batchDeleteInDB({
	  url:getBasePath()+"/"+modelLowClassName+"/batchDel"+modelUpClassName+".do",
  });
}
         
         
 //恢复数据
function rebackData(){
	$.dataGridEasyUI.batchDeleteInDB({
		  url:basePath+"/"+modelLowClassName+"/batchRebackData.do"
	});
}
s